//
//  screenViewController.m
//  SelfDriving
//
//  Created by apple on 16/2/4.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "screenViewController.h"
#import "UIChooseTableView.h"
#import "BusinessService+User.h"
#import "CityViewController.h"
#import "QryResultViewController.h"
@interface screenViewController ()<UIChooseTableViewDelegate,CityViewControllerDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *mainview;
@property (weak, nonatomic) IBOutlet UIView *bgview;
@property (weak, nonatomic) IBOutlet UIView *queview;
@property (weak, nonatomic) IBOutlet UIButton *qbtn;
@property (weak, nonatomic) IBOutlet UIButton *okbtn;
@property (weak, nonatomic) IBOutlet UILabel *ageLable;
@property (weak, nonatomic) IBOutlet UILabel *xzLable;
@property (weak, nonatomic) IBOutlet UILabel *bdLable;
@property (weak, nonatomic) IBOutlet UILabel *gxLable;
@end

@implementation screenViewController{
    UIChooseTableView *chooseTabel;
    NSMutableDictionary *dic;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    dic = [[NSMutableDictionary alloc]init];
    _mainview.layer.cornerRadius = 4;
    _bgview.frame = self.view.frame;
    _bgview.layer.opacity = 0.8;
    _queview.layer.cornerRadius = 4;
    _qbtn.layer.cornerRadius = 4;
    _okbtn.layer.cornerRadius = 4;
//    self.view.layer.borderColor = 
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymgender"] isEqualToString:@"1"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:1];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
//        [dic setObject:@"1" forKey:@"gender"];
        //xhw11月7号改
        [dic setObject:@"2" forKey:@"gender"];
    }
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymgender"] isEqualToString:@"2"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:2];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
//        [dic setObject:@"2" forKey:@"gender"];
        [dic setObject:@"0" forKey:@"gender"];
    }
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymgender"] isEqualToString:@"3"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:3];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
        [dic setObject:@"1" forKey:@"gender"];
    }
    
    
    
    
//    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymlogintime"] isEqualToString:@"4"]) {
//        UIButton *btn = (UIButton *)[self.view viewWithTag:4];
//        btn.backgroundColor = UIColorFromRGB(0xdadada);
//        [dic setObject:@"900" forKey:@"logintime"];
//    }
    //将15分钟改为全部(xhw11月20号改)
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymlogintime"] isEqualToString:@"4"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:4];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
//        [dic setObject:@"" forKey:@"logintime"];
        [dic removeObjectForKey:@"logintime"];
    }
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymlogintime"] isEqualToString:@"5"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:5];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
        [dic setObject:@"3600" forKey:@"logintime"];
    }
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymlogintime"] isEqualToString:@"6"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:6];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
        [dic setObject:@"86400" forKey:@"logintime"];
    }
    if ([[UserDefaults readUserDefaultObjectValueForKey:@"ymlogintime"] isEqualToString:@"7"]) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:7];
        btn.backgroundColor = UIColorFromRGB(0xdadada);
        [dic setObject:@"259200" forKey:@"logintime"];
    }
}
#pragma mark 年龄选择
- (IBAction)ageAction:(UIButton *)sender {
    NSLog(@"%@",[AppDelegate shareDelegate].user.ismember);
    LoginResponseModel *user = [AppDelegate shareDelegate].user;
    NSString *ismember = [AppDelegate shareDelegate].user.memberlevel;
    if ([[AppDelegate shareDelegate].user.memberlevel intValue] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"会员才能查看此项，是否成为会员？" delegate:self cancelButtonTitle:@"成为会员" otherButtonTitles:@"取消", nil];
        [alert show];
        return;
    }
    chooseTabel = [[UIChooseTableView alloc] initWithFrame:CGRectMake(0.0f, SCRE_HEIGHT, SCRE_WIDTH, 260.0f)];
    chooseTabel.delegate = self;
    chooseTabel.lanel_title.text = @"选择年龄";
    NSMutableArray *ary = [[NSMutableArray alloc]initWithObjects:@{@"name":@"18-22岁",@"id":@"18-22"},
                           @{@"name":@"23-26岁",@"id":@"23-26"},
                           @{@"name":@"27-35岁",@"id":@"27-35"},
                           @{@"name":@"36以上",@"id":@"36"},nil];
    chooseTabel.mainData  = ary;
    [chooseTabel up];
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:chooseTabel];
    
}
#pragma  mark 快速导航-点击回调
-(void)didSelectDate:(UITableView *)chooseTableView indexPath:(NSIndexPath *)indexPath  string:(NSMutableDictionary *)string{
    
    if ([chooseTabel.lanel_title.text isEqualToString:@"选择年龄"]) {
        _ageLable.text  = [string objectForKey:@"name"];
        [dic setObject:[ValidateHelper stringAge:[string objectForKey:@"id"]] forKey:@"age"];
    }else if([chooseTabel.lanel_title.text isEqualToString:@"选择星座"]){
        _xzLable.text = [string objectForKey:@"name"];
        [dic setObject:_xzLable.text forKey:@"xingzuo"];
    }else if([chooseTabel.lanel_title.text isEqualToString:@"选择绑定"]){
        _bdLable.text = [string objectForKey:@"name"];
        [dic setObject:[string objectForKey:@"id"] forKey:@"bingding"];
    }else if([chooseTabel.lanel_title.text isEqualToString:@"选择故乡"]){
        _gxLable.text = [string objectForKey:@"name"];
        [dic setObject:_gxLable.text forKey:@"profsn"];
    }
}

#pragma mark 星座选择
- (IBAction)xinzAction:(UIButton *)sender {
    if ([[AppDelegate shareDelegate].user.memberlevel intValue] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"会员才能查看此项，是否成为会员？" delegate:self cancelButtonTitle:@"成为会员" otherButtonTitles:@"取消", nil];
        [alert show];
        return;
    }
    
    
    
    [[BusinessService shareInstance] xingzuoLists:[[NSMutableDictionary alloc]init] success:^(id result) {
        NSMutableArray *ary = result;
        if (ary.count!=0) {
            chooseTabel = [[UIChooseTableView alloc] initWithFrame:CGRectMake(0.0f, SCRE_HEIGHT, SCRE_WIDTH, 260.0f)];
            chooseTabel.delegate = self;
            chooseTabel.mainData  = result;
            chooseTabel.lanel_title.text = @"选择星座";
            [chooseTabel up];
            [[[[UIApplication sharedApplication] windows] firstObject] addSubview:chooseTabel];
        }
    } failure:^(id result) {
        
    }];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self dismissViewControllerAnimated:NO completion:nil];
        [[NSNotificationCenter defaultCenter ]postNotificationName:@"toVipCenteVC" object:nil];
    }
    
}
#pragma mark 绑定选择
- (IBAction)bdAction:(UIButton *)sender {
    if ([[AppDelegate shareDelegate].user.memberlevel intValue] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"会员才能查看此项，是否成为会员？" delegate:self cancelButtonTitle:@"成为会员" otherButtonTitles:@"取消", nil];
        [alert show];
        return;
    }
//    chooseTabel = [[UIChooseTableView alloc] initWithFrame:CGRectMake(0.0f, SCRE_HEIGHT, SCRE_WIDTH, 260.0f)];
    
    chooseTabel = [[UIChooseTableView alloc] initWithFrame:CGRectMake(0.0f, SCRE_HEIGHT, SCRE_WIDTH, 160.0f)];
    chooseTabel.delegate = self;
    chooseTabel.lanel_title.text = @"选择绑定";
    NSMutableArray *ary = [[NSMutableArray alloc]initWithObjects:@{@"name":@"新浪微博",@"id":@"1"},
                           @{@"name":@"腾讯账号",@"id":@"2"},
                           @{@"name":@"微信账号",@"id":@"3"},nil];
    chooseTabel.mainData  = ary;
    [chooseTabel up];
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:chooseTabel];
    
}
#pragma mark 故乡选择
- (IBAction)gxAction:(id)sender {
    if ([[AppDelegate shareDelegate].user.memberlevel intValue] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"会员才能查看此项，是否成为会员？" delegate:self cancelButtonTitle:@"成为会员" otherButtonTitles:@"取消", nil];
        [alert show];
        return;
    }
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"U"
                                                                 bundle:[NSBundle mainBundle]];
    CityViewController *viewcv = [customerStoryboard instantiateViewControllerWithIdentifier:@"CityViewController"];
    viewcv.view.backgroundColor = [UIColor clearColor];
    viewcv.delegate = self;
    viewcv.modalPresentationStyle= UIModalPresentationOverFullScreen;
    [self presentViewController:viewcv animated:YES completion:nil];
}
#pragma mark 故乡回调
-(void)didSelectDate:(NSString *)name id:(NSString *)id{
    _gxLable.text = name;
    [dic setObject:id forKey:@"hometown"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)suserAction:(UIButton *)sender {
    for (int i = 0; i<3; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:i+1];
        btn.backgroundColor = [UIColor whiteColor];
    }
    sender.backgroundColor = UIColorFromRGB(0xdadada);
    if (sender.tag == 1) {//全部
        [dic setObject:@"2" forKey:@"gender"];
        [UserDefaults writeUserDefaultObjectValue:@"1" withKey:@"ymgender"];
    }else if (sender.tag == 2){//男
        [dic setObject:@"0" forKey:@"gender"];
        [UserDefaults writeUserDefaultObjectValue:@"2" withKey:@"ymgender"];
    }else if (sender.tag == 3){//女
        [dic setObject:@"1" forKey:@"gender"];
        [UserDefaults writeUserDefaultObjectValue:@"3" withKey:@"ymgender"];
    }
}
- (IBAction)stimeAction:(id)sender {
    UIButton *btns = (UIButton *)sender;
    for (int i = 0; i<4; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:3+(i+1)];
        btn.backgroundColor = [UIColor whiteColor];
    }
    btns.backgroundColor = UIColorFromRGB(0xdadada);
//    if (btns.tag == 4) {//15分钟
//        [dic setObject:@"900" forKey:@"logintime"];
//        [UserDefaults writeUserDefaultObjectValue:@"4" withKey:@"ymlogintime"];
//    }
    if (btns.tag == 4) {//改为全部
//        [dic setObject:@"" forKey:@"logintime"];
        [dic removeObjectForKey:@"logintime"];
        [UserDefaults writeUserDefaultObjectValue:@"4" withKey:@"ymlogintime"];
    }
    else if (btns.tag == 5){//60分钟
        [dic setObject:@"3600" forKey:@"logintime"];
        [UserDefaults writeUserDefaultObjectValue:@"5" withKey:@"ymlogintime"];
    }else if (btns.tag == 6){//1天
        [dic setObject:@"86400" forKey:@"logintime"];
        [UserDefaults writeUserDefaultObjectValue:@"6" withKey:@"ymlogintime"];
    }else if(btns.tag == 7){//3天
        [dic setObject:@"259200" forKey:@"logintime"];
        [UserDefaults writeUserDefaultObjectValue:@"7" withKey:@"ymlogintime"];
    }
}
- (IBAction)comitAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"qryAction" object:dic];
}

- (IBAction)backAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



@end
